/**
 * 根据难度设置全局变量
 * @param danger: 感染（危险）程度
 * @param currentSpeed: 感染速度
 * @param injection: 剩余次数：打针
 * @param injectionEffect: 治愈百分比-打针
 * @param drug: 剩余次数：吃药
 * @param drugEffect: 治愈百分比-吃药
 * @param elixir: 剩余次数：研究
 * @param diagnosis: 剩余次数：诊断
 * @param diagnosisEffect: xx秒之内暂停感染
 * @param quarantine: 剩余次数：隔离
 * @param quarantineEffect: xx秒之内增长减缓50%
 * @param observation: 剩余次数：观察
 * @param observationEffect: xx秒之内增长减缓30%
 * @param questionmaxnum: 最大答题数
 */
//简单（实习）难度
var level_1 = {
    defaultDanger: 30.00,
    danger: 30.00,
    defaultSpeed: 0.1,
    injection: 3,
    injectionEffect: 25,
    drug: 3,
    drugEffect: 15,
    elixir: 3,
    diagnosis: 3,
    diagnosisEffect: 15,
    quarantine: 3,
    quarantineEffect: 15,
    observation: 3,
    observationEffect: 15,
    questionmaxnum: 1
};
//中等（主治）难度
var level_2 = {
    defaultDanger: 45.00,
    danger: 45.00,
    defaultSpeed: 0.2,
    injection: 2,
    injectionEffect: 20,
    drug: 2,
    drugEffect: 10,
    elixir: 2,
    diagnosis: 2,
    diagnosisEffect: 10,
    quarantine: 2,
    quarantineEffect: 10,
    observation: 2,
    observationEffect: 10,
    questionmaxnum: 2
};
//困难（专家）难度
var level_3 = {
    defaultDanger: 60.00,
    danger: 60.00,
    defaultSpeed: 0.3,
    injection: 1,
    injectionEffect: 15,
    drug: 1,
    drugEffect: 5,
    elixir: 1,
    diagnosis: 1,
    diagnosisEffect: 5,
    quarantine: 1,
    quarantineEffect: 5,
    observation: 1,
    observationEffect: 5,
    questionmaxnum: 3
};

//总人数
var allnum = 0;

//感染程度计时器
var currentSpeed;
var dangerInterval;
var dangerSetTimeOut;
var diagnosisInterval;
var quarantineInterval;
var observationInterval;
var diagnosisTime = 0;
var quarantineTime = 0;
var observationTime = 0;


/**
 * 根据难度获取全局配置
 * @param level
 * @returns {{defaultDanger: number, elixir: number, quarantineEffect: number, observation: number, diagnosis: number, danger: number, injectionEffect: number, drug: number, drugEffect: number, defaultSpeed: number, diagnosisEffect: number, quarantine: number, observationEffect: number, injection: number}}
 */
function getLevelOptions(level) {
    var option;
    switch (level) {
        case '1': option = level_1;break;
        case '2': option = level_2;break;
        case '3': option = level_3;break;
        default: option = level_1;
    }
    return option;
}

var sysConfig = {
    gameTime: 3 * 60,
    cure: 0,
    death: 0,
    option: getLevelOptions(getCookie("level"))
};

/**
 * 增加感染程度，页面渲染
 */
function infected() {
    dangerInterval= setInterval(function () {
        sysConfig.option.danger += currentSpeed;
        index_set_color(sysConfig.option.danger);
    }, 100);
}

/**
 * 打针
 */
function useInjection() {
    var option = sysConfig.option;
    if(option.injection > 0){
        option.injection --;
        var danger = option.danger - option.injectionEffect;
        if(danger >= 0){
            option.danger = danger;
        }else{
            option.danger = 0;
        }
        index_set_color(option.danger);
        updateInjection(option);
        outjs_openblock('针剂-1，病情恶化率降低' + option.injectionEffect + "%...");
    }
}

/**
 * 渲染剩余数量-打针
 */
function updateInjection() {
    $("#injection").text(sysConfig.option.injection);
}

/**
 * 吃药
 */
function useDrug() {
    var option = sysConfig.option;
    if(option.drug > 0){
        option.drug --;
        var danger = option.danger - option.drugEffect;
        if(danger >= 0){
            option.danger = danger;
        }else{
            option.danger = 0;
        }
        index_set_color(option.danger);
        updateDrug(option);
        outjs_openblock('药品-1，病情恶化率降低' + option.drugEffect + "%...");
    }
}

/**
 * 渲染剩余数量-吃药
 */
function updateDrug() {
    $("#drug").text(sysConfig.option.drug);
}

/**
 * 使用特效药
 */
function useElixir() {
    var option = sysConfig.option;
    if(option.elixir > 0){
        option.elixir --;
        updateElixir(option);
        cure();
    }
}

/**
 * 渲染剩余数量-特效药
 */
function updateElixir() {
    $("#elixir").text(sysConfig.option.elixir);
}

/**
 * 诊断
 */
function useDiagnosis() {
    var option = sysConfig.option;
    if(option.diagnosis > 0){
        quarantineTime = 0;
        observationTime = 0;
        option.diagnosis --;
        clearInterval(dangerInterval);
        clearTimeout(dangerSetTimeOut);
        clearInterval(diagnosisInterval);
        diagnosisTime += option.diagnosisEffect;
        dangerSetTimeOut = setTimeout(infected, diagnosisTime * 1000);
        diagnosisInterval = setInterval(function () {
            diagnosisTime --;
            if( diagnosisTime == 0 ){
                clearInterval(diagnosisInterval);
            }
        },1000);
        updateDiagnosis(option);
        outjs_openblock(diagnosisTime + '秒内病情停止恶化...');
    }
}

/**
 * 渲染剩余数量-诊断
 */
function updateDiagnosis() {
    $("#diagnosis").text(sysConfig.option.diagnosis);
}

/**
 * 隔离
 */
function useQuarantine() {
    var option = sysConfig.option;
    if(option.quarantine > 0){
        diagnosisTime = 0;
        observationTime = 0;
        option.quarantine --;
        clearInterval(dangerInterval);
        clearTimeout(dangerSetTimeOut);
        currentSpeed = 0.5 * option.defaultSpeed;
        quarantineTime += option.quarantineEffect;
        infected();
        dangerSetTimeOut = setTimeout(function () {
            clearInterval(dangerInterval);
            currentSpeed = option.defaultSpeed;
            infected();
        },quarantineTime * 1000);
        quarantineInterval = setInterval(function () {
            quarantineTime --;
            if( quarantineTime == 0 ){
                clearInterval(quarantineInterval);
            }
        },1000);
        updateQuarantine(option);
        outjs_openblock(quarantineTime + '秒内病情恶化速度降低50%...');
    }
}

/**
 * 渲染剩余数量-隔离
 */
function updateQuarantine() {
    $("#quarantine").text(sysConfig.option.quarantine);
}

/**
 * 观察
 */
function useObservation() {
    var option = sysConfig.option;
    if(option.observation > 0){
        diagnosisTime = 0;
        quarantineTime = 0;
        option.observation --;
        clearInterval(dangerInterval);
        clearTimeout(dangerSetTimeOut);
        currentSpeed = 0.3 * option.defaultSpeed;
        observationTime += option.observationEffect;
        infected();
        dangerSetTimeOut = setTimeout(function () {
            clearInterval(dangerInterval);
            currentSpeed = option.defaultSpeed;
            infected();
        },observationTime * 1000);
        observationInterval = setInterval(function () {
            observationTime --;
            if( observationTime == 0 ){
                clearInterval(observationInterval);
            }
        },1000);
        updateObservation(option);
        outjs_openblock(observationTime + '秒内病情恶化速度降低30%...');
    }
}

/**
 * 渲染剩余数量-观察
 */
function updateObservation() {
    $("#observation").text(sysConfig.option.observation);
}

/**
 * 治愈成功
 */
function cure() {
    sysConfig.cure++;
    sysConfig.option.danger = sysConfig.option.defaultDanger;
    $("#successNumber").text(sysConfig.cure);
    index_set_color(sysConfig.option.danger);
    outjs_openblock("治愈+1...");
    allnum = sysConfig.death + sysConfig.cure;
    change_hbg();
}

/**
 * 治愈失败
 */
function death() {
    sysConfig.death++;
    sysConfig.option.danger = sysConfig.option.defaultDanger;
    $("#failNumber").text(sysConfig.death);
    outjs_openblock("死亡+1...");
    allnum = sysConfig.death + sysConfig.cure;
    change_hbg();
}

/**
 * 开始游戏
 */
function gameStart() {
    var option = sysConfig.option;
    currentSpeed = option.defaultSpeed;
    index_set_color(option.danger);
    infected();
    updateInjection();
    updateDrug();
    updateElixir();
    updateDiagnosis();
    updateQuarantine();
    updateObservation();
    titleUpdate();
    var game = setInterval(function () {
        sysConfig.gameTime --;
        titleUpdate();
        if( sysConfig.gameTime == 0 ){
            gameOver();
            clearInterval(game);
        }
    }, 1000);
}

/**
 * 游戏结束
 */
function gameOver() {
    clearInterval(dangerInterval);
    clearTimeout(dangerSetTimeOut);
    doctor_gameover();
}

/**
 * 去排行榜页面
 */
function toRank() {
    var cureRate;
    if(sysConfig.cure + sysConfig.death == 0){
        cureRate = 0;
    }else{
        cureRate = (sysConfig.cure/( sysConfig.cure + sysConfig.death )).toFixed(4) * 100;
    }
    setCookie("score", cureRate);
    var data = {
        score: cureRate,
        userDiff: getCookie("level"),
        username: getCookie("userName")
    }
    ajaxCommit("POST", "http://www.jxapp.com:8080/game/submitCore", data, function (res) {
        if(res.ok){
            window.location.href = "./rank.html";
        }else{
            outjs_openblock("保存失败");
        }
    });
}

/**
 * 治愈、失败、游戏剩余时间赋值
 */
function titleUpdate() {
    $("#failNumber").text(sysConfig.death);
    $("#successNumber").text(sysConfig.cure);
    $("#gameTime").text(sysConfig.gameTime);
    $("#gameTime2").text(sysConfig.gameTime);
    
}

/**
 * 变换色值
 */
function change_hbg()
{
    
    //变换人物色彩
    var bgcol = new Array();
    bgcol[0] = '#1cb1b1';
    bgcol[1] = '#1cb1ff';
    bgcol[2] = '#00ac11';
    bgcol[3] = '#b168ff';
    var l = bgcol.length;
    var g = allnum % l;
    var bg = bgcol[g];
    $('.mbb_rw_bg').css({'background':bg});

    //变换病名
    var bnamelist = new Array();
    bnamelist[0] = '糖尿病患者';
    bnamelist[1] = '高血压患者';
    bnamelist[2] = '癌症患者';
    bnamelist[3] = '痛风患者';
    bnamelist[4] = '哮喘患者';
    bnamelist[5] = '抑郁症患者';
    bnamelist[6] = '肺结核患者';
    bnamelist[7] = '痴呆患者';
    bnamelist[8] = '亚健康患者';
    var l2 = bnamelist.length;
    var g2 = allnum % l2;
    var bname = bnamelist[g2];
    $('.mbb_txt_2').html('第'+(allnum+1)+'个<br />'+bname);
}